const {createStore} = require("redux");

//定义一个reducer函数，接收一个state和一个action作为参数
//参数一:store中保存的初始状态
//参数二:action,描述要执行的动作,本次需要更新的action
//返回值:它的返回值会作为store之后存储的状态
function reducer(state = initialState, action) {
    if (action.type === "CHANGE_NAME") {
        return {...state, name: action.name}
    }
    if (action.type === "CHANGE_AGE") {
        return {...state, age: action.age}
    }
    // 返回初始状态
    return initialState;
}

const initialState = {name: "wsb", age: 18};

const store = createStore(reducer);


module.exports = store;

const nameAction = {type: "CHANGE_NAME", name: "db"}
store.dispatch(nameAction)

const ageAction = {type: "CHANGE_AGE", age: 17}
store.dispatch(ageAction)

store.subscribe(() => {
    console.log("订阅变化后的数据", store.getState())
})